package com.example.administrator.smartdevice.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.base.BaseActivity;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class DeviceAddActivity extends BaseActivity {
    @BindView(R.id.img)
    protected ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_device_add);
    }

    @Override
    protected void initData() {
        String s = getJsonString("00000000", "11111111", "22222222");
        CreateQRCodeTask e = new CreateQRCodeTask(this, s);
        e.execute();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {

    }

    public String getJsonString(String deviceId, String applianceId, String applianceType) {
        return "{\"deviceId\":\"" + deviceId + "\",\"applianceId\":\"" + applianceId + "\",\"applianceType\":\"" + applianceType + "\"}";
    }

    static class CreateQRCodeTask extends AsyncTask<Void, Void, Bitmap> {
        // 弱引用是允许被gc回收的;
        private final WeakReference<DeviceAddActivity> mActivity;
        public String mContent;

        CreateQRCodeTask(DeviceAddActivity mActivity, String content) {
            this.mActivity = new WeakReference<>(mActivity);
            this.mContent = content;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            Bitmap logoBitmap = BitmapFactory.decodeResource(mActivity.get().getResources(), R.mipmap.icon_logo1);
            return QRCodeEncoder.syncEncodeQRCode(mContent, BGAQRCodeUtil.dp2px(mActivity.get(), 200), Color.BLACK, Color.WHITE, logoBitmap);

        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            DeviceAddActivity activity = mActivity.get();
            if (activity == null
                    || activity.isFinishing()
                    || activity.isDestroyed()) {
                // activity没了,就结束可以了
                return;
            }
            if (bitmap != null) {
                //成功创建，上传到服务器
                mActivity.get().img.setImageBitmap(bitmap);
            } else {
                Toast.makeText(mActivity.get(), "生成带logo的英文二维码失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
